package com.hl.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.hl.domain.ProductCategory;
import com.hl.mapper.ProductApprovalMapper;
import com.hl.domain.ProductApproval;
import com.hl.service.IProductApprovalService;

/**
 * 商品审核Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-07
 */
@Service
public class ProductApprovalServiceImpl implements IProductApprovalService 
{
    @Autowired
    private ProductApprovalMapper productApprovalMapper;

    /**
     * 查询商品审核
     * 
     * @param id 商品审核主键
     * @return 商品审核
     */
    @Override
    public ProductApproval selectProductApprovalById(Integer id)
    {
        return productApprovalMapper.selectProductApprovalById(id);
    }

    /**
     * 查询商品审核列表
     * 
     * @param productApproval 商品审核
     * @return 商品审核
     */
    @Override
    public List<ProductApproval> selectProductApprovalList(ProductApproval productApproval)
    {
        return productApprovalMapper.selectProductApprovalList(productApproval);
    }

    /**
     * 新增商品审核
     * 
     * @param productApproval 商品审核
     * @return 结果
     */
    @Transactional
    @Override
    public int insertProductApproval(ProductApproval productApproval)
    {
        productApproval.setCreateTime(DateUtils.getNowDate());
        int rows = productApprovalMapper.insertProductApproval(productApproval);
        insertProductCategory(productApproval);
        return rows;
    }

    /**
     * 修改商品审核
     * 
     * @param productApproval 商品审核
     * @return 结果
     */
    @Transactional
    @Override
    public int updateProductApproval(ProductApproval productApproval)
    {
        productApproval.setUpdateTime(DateUtils.getNowDate());
        productApprovalMapper.deleteProductCategoryById(productApproval.getId());
        insertProductCategory(productApproval);
        return productApprovalMapper.updateProductApproval(productApproval);
    }

    /**
     * 批量删除商品审核
     * 
     * @param ids 需要删除的商品审核主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteProductApprovalByIds(Integer[] ids)
    {
        productApprovalMapper.deleteProductCategoryByIds(ids);
        return productApprovalMapper.deleteProductApprovalByIds(ids);
    }

    /**
     * 删除商品审核信息
     * 
     * @param id 商品审核主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteProductApprovalById(Integer id)
    {
        productApprovalMapper.deleteProductCategoryById(id);
        return productApprovalMapper.deleteProductApprovalById(id);
    }

    /**
     * 新增商品分类信息
     * 
     * @param productApproval 商品审核对象
     */
    public void insertProductCategory(ProductApproval productApproval)
    {
        List<ProductCategory> productCategoryList = productApproval.getProductCategoryList();
        Integer id = productApproval.getId();
        if (StringUtils.isNotNull(productCategoryList))
        {
            List<ProductCategory> list = new ArrayList<ProductCategory>();
            for (ProductCategory productCategory : productCategoryList)
            {
                productCategory.setId(id);
                list.add(productCategory);
            }
            if (list.size() > 0)
            {
                productApprovalMapper.batchProductCategory(list);
            }
        }
    }
}
